package com.example.simpleforum.servlet;

import com.example.simpleforum.dao.ForumAdminRelationDao;
import com.example.simpleforum.dao.ReplyDao;
import com.example.simpleforum.model.User;
import com.example.simpleforum.model.User.Role;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 删除回复Servlet
 */
public class ReplyDeleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前登录用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        
        if (user == null) {
            // 用户未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 获取回复ID
        String replyIdStr = request.getParameter("id");
        if (replyIdStr == null || replyIdStr.trim().isEmpty()) {
            // 回复ID为空，重定向到帖子列表页面
            response.sendRedirect(request.getContextPath() + "/PostListServlet");
            return;
        }
        
        try {
            int replyId = Integer.parseInt(replyIdStr);
            
            // 查询回复详情以获取帖子ID
            ReplyDao replyDao = new ReplyDao();
            int postId = -1;
            try {
                postId = replyDao.findById(replyId).getPostId();
            } catch (NullPointerException e) {
                // 回复不存在，重定向到帖子列表页面
                response.sendRedirect(request.getContextPath() + "/PostListServlet");
                return;
            }
            
            boolean success = false;
            
            // 根据用户角色执行不同的删除操作
            if (user.getRole() == Role.main_admin) {
                // 主管理员可以删除任何回复
                success = replyDao.deleteReplyByMainAdmin(replyId);
            } else if (user.getRole() == Role.forum_admin) {
                // 板块管理员可以删除自己管理的板块中的回复
                int forumId = replyDao.getForumIdByReplyId(replyId);
                if (forumId != -1) {
                    ForumAdminRelationDao forumAdminDao = new ForumAdminRelationDao();
                    if (forumAdminDao.isForumAdmin(forumId, user.getId())) {
                        success = replyDao.deleteReplyByForumAdmin(replyId, forumId);
                    }
                }
            } else {
                // 普通用户只能删除自己的回复
                success = replyDao.deleteReply(replyId, user.getId());
            }
            
            // 无论删除成功与否，都重定向到帖子详情页面
            response.sendRedirect(request.getContextPath() + "/PostDetailServlet?id=" + postId);
            
        } catch (NumberFormatException e) {
            // 回复ID格式错误，重定向到帖子列表页面
            response.sendRedirect(request.getContextPath() + "/PostListServlet");
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // POST请求也执行相同操作
        doGet(request, response);
    }
}